package com.bkkp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bkkp.R;
import com.common.pojo.bkkp.Category;

import java.util.List;

public class CategoryAdapter extends BaseAdapter {
    private Context context;
    private List<Category> categoryList;

    public CategoryAdapter(Context context, List<Category> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
    }

    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category, parent, false);
            holder = new ViewHolder();
            holder.iconImageView = convertView.findViewById(R.id.iconImageView);
            holder.nameTextView = convertView.findViewById(R.id.nameTextView);
            holder.selectedView = convertView.findViewById(R.id.selectedView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Category category = categoryList.get(position);
        holder.iconImageView.setImageResource(category.getIconResId());
        holder.nameTextView.setText(category.getName());

        // 根据选中状态设置UI
        if (category.isSelected()) {
            holder.nameTextView.setTextColor(context.getResources().getColor(R.color.primary));
            holder.selectedView.setVisibility(View.VISIBLE);
        } else {
            holder.nameTextView.setTextColor(context.getResources().getColor(R.color.text_secondary));
            holder.selectedView.setVisibility(View.GONE);
        }

        return convertView;
    }

    private static class ViewHolder {
        ImageView iconImageView;
        TextView nameTextView;
        View selectedView;
    }
}
